<?php
/**
 * Created by PhpStorm.
 * User: 胥九庆
 * Date: 2023/01/09
 * Time: 11:02
 */
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;
use app\common\tool\account\Account;

class ContractRelease extends Base
{
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function getStatusAttr($value)
    {
        return releaseState($value);
    }

    /**
     * 记录释放信息
     */
    public function record($uid, $amount)
    {
        try {
            $list = Relation::where(['new_uid' => $uid])
                ->field('uid, algebra')->order('algebra', 'asc')->select();
            if ($list) {
                $time = time();
                $day = Cache::get('system')['team_subsidy_day'];
                $data = [];
                $i = 1;
                foreach ($list as $val) {
                    $super_cid = User::where('id', $val['uid'])->value('contract_role_id');
                    if ($super_cid > 0 && $i <= 2) {
                        $rate = Cache::get('system')['team_subsidy_rate_' . $i];
                        $amount_sum = roundNum($amount * $rate, 2);
                        $day_amount = floor($amount_sum / $day * 100) / 100;
                        $data[] = [
                            'uid' => $val->uid,
                            'source' => $uid,
                            'amount_sum' => $amount_sum,
                            'amount' => 0,
                            'day' => $day,
                            'surplus_day' => $day,
                            'day_amount' => $day_amount,
                            'create_time' => $time,
                            'update_time' => $time
                        ];
                        $i++;
                    }
                }
                if ($data) {
                    $this->insertAll($data);
                }
            }

        } catch (\Exception $e) {

            return $e->getMessage();
        }

        return true;
    }

    /**
     * 每日释放-1002团队津贴
     * 定时
     */
    public static function dailyRelease(): array
    {
        parent::startTrans();
        try {
            $list = parent::where(['status' => 1, 'is_del' => 0])->where('surplus_day', '>', 0)->select();
            if ($list) {
                foreach ($list as $val) {
                    $real_amount = $val->day_amount;
                    if ($val->surplus_day == 1) {
                        $real_amount = roundNum($val->amount_sum - $val->amount, 2);
                        $val->status = 2;
                    }
                    $val->surplus_day = $val->surplus_day - 1;
                    $val->amount = roundNum($val->amount + $real_amount, 2);
                    $val->save();
                    if ($real_amount > 0) {
                        Account::getInstance()->basics($val->uid, 'subsidy', $real_amount, '团队津贴', '+')
                            ->createBonus($val->source, 1002);
                    }
                }
            }
            parent::commit();
        } catch (\Exception $e) {
            parent::rollback();
            Logs::write('logs/contract-dailyRelease.log', $e->getMessage());
            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '每日释放成功'];
    }


}